package com.nb.linkedlist;

/**
 * @author lihaoyang
 * @date 2021/12/28
 */
public class ReverseLinkedList {

    public static void main(String[] args) {

        MyNode node01 = new MyNode(1);
        MyNode node02 = new MyNode(2);
        MyNode node03 = new MyNode(3);
        MyNode node04 = new MyNode(4);
        node01.setNext(node02);
        node02.setNext(node03);
        node03.setNext(node04);
        node04.setNext(null);

        printNode(node01);
        node01 = reverse(node01);
        printNode(node01);
    }


    public static MyNode reverse(MyNode node){

        MyNode pre = null;
        MyNode cur = node;
        while (cur  !=null){
            MyNode curnext=cur.next;
            cur.next = pre;
            pre=cur;
            cur=curnext;
        }
        return pre;
    }







    public static void printNode(MyNode node){
        if(node == null){
            return;
        }
        while (node != null){
            System.out.print(node.getValue()+" -> ");
            node = node.getNext();
        }
        System.out.println();
    }
}
